package com.zhengxing.jienci.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhengxing.jienci.user.service.IRankPointService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.zhengxing.jienci.common.pojo.CustomizePage;
import com.zhengxing.jienci.common.support.SimpleResponse;
import com.zhengxing.jienci.common.util.IdGenerateUtil;
import com.zhengxing.jienci.user.domain.RankPoint;
import java.util.*;

/**
 * <p>
 * 积分等级对照表 前端控制器
 * </p>
 *
 * @author yyh
 * @since 2020-12-02
 */
@Api(tags = "积分等级对照表")
@RestController
@RequestMapping("/rankPoint")
public class RankPointController {

	@Autowired
	private IRankPointService iRankPointService;

	/**
	 * 通过id查询
	 */
	@ApiOperation("获取详情")
	@GetMapping("/info/{id}")
	public RankPoint getById(@PathVariable(value = "id") String id) {
		return iRankPointService.getById(id);
	}

	/**
	 * 新增
	 */
	@ApiOperation("新增操作")
	@PostMapping("/save")
	public SimpleResponse save(@RequestBody RankPoint rankPoint) {
		rankPoint.setId(IdGenerateUtil.getId());
		iRankPointService.save(rankPoint);
		return SimpleResponse.success();
	}

	/**
	 * 通过id删除
	 */
	@ApiOperation("删除操作")
	@DeleteMapping("/delete/{id}")
	public SimpleResponse delete(@PathVariable(value = "id") String ids) {
		String[] idsStrs = ids.split(",");
		for (String id : idsStrs) {
			iRankPointService.removeById(id);
		}
		return SimpleResponse.success();
	}

	/**
	 * 修改
	 */
	@ApiOperation("修改操作")
	@PutMapping("/update")
	public SimpleResponse updateById(@RequestBody RankPoint rankPoint) {
		iRankPointService.updateById(rankPoint);
		return SimpleResponse.success();
	}

	/**
	 * 查询列表
	 */
	@ApiOperation("列表查询")
	@GetMapping("/list")
	public List<RankPoint> list(RankPoint rankPoint) {
		final LambdaQueryWrapper<RankPoint> lambda = new QueryWrapper<RankPoint>().lambda();
		this.buildCondition(lambda, rankPoint);
		return iRankPointService.list(lambda);
	}

	/**
	 * 分页查询
	 */
	@ApiOperation("分页查询")
	@GetMapping("/page")
	public IPage<RankPoint> page(CustomizePage page, RankPoint param) {
		final LambdaQueryWrapper<RankPoint> lambda = new QueryWrapper<RankPoint>().lambda();
		this.buildCondition(lambda, param);
		Page<RankPoint> pages = new Page<RankPoint>(page.getPageNow(), page.getPageSize());
		IPage<RankPoint> resultPage = iRankPointService.page(pages, lambda);
		return resultPage;
	}

	/**
	 * 构造查询条件
	 * 
	 * @param lambda
	 * @param param
	 */
	@SuppressWarnings("unchecked")
	private void buildCondition(LambdaQueryWrapper<RankPoint> lambda, RankPoint param) {
		if (!StringUtils.isEmpty(param.getId())) {
			lambda.eq(RankPoint::getId, param.getId());
		}
		if (!StringUtils.isEmpty(param.getRank())) {
			lambda.eq(RankPoint::getRank, param.getRank());
		}
		if (!StringUtils.isEmpty(param.getMinPoint())) {
			lambda.eq(RankPoint::getMinPoint, param.getMinPoint());
		}
		lambda.orderBy(true, false, RankPoint::getRank);
	}

}
